<?php
 
    extract(extractGet(), EXTR_PREFIX_ALL, 'u');
    if(isset($u_getAllData))
        $getResultAsArray = $u_getAllData;
    else $getResultAsArray = 0;
    
    //Preverimo podanost spremenljivk
    if(isset($u_email) && isset($u_password)){
        $userTest = R::findOne('user', 'email = ?', [$u_email]);
        if($userTest && isUserLocked($userTest->lockTime)){
            jsonErrorDie("This user account is locked. Please try again in 5 minutes.");
        }
        $user = R::findOne('user', 'email = ? AND password = ?', [$u_email, $u_password]);

        if($user && isUserLocked($user->lockTime)){
            jsonErrorDie("This user account is locked. Please try again in 5 minutes.");
        }
        if($user && !$user->active){
            jsonErrorDie("Requested user is inactive.");
        }
        
        if($user && $user->active) 
        {
            $user->possibleRoles = getPossibleUserRoles($user->id);

            // ČE zahtevamo vse podatke
            if($getResultAsArray){
                // Pogledamo rolo userja, uporabno za vračanje userju prilagodljivih informacij - recimo samo njegove tabele
                $loginUserID = $user->id;
                $userOnLogin = $user;
                foreach($user->sharedRoleList as $role){
                    if($role->id == 1){
                        $loginUserID = "ADMIN";
                        continue;
                    }
                }
                
                include "./userManagement/getUsers.php";
                $result["allUsers"] = $users;
                
                include "./projectManagement/getProjects.php";
                $result["allProjects"] = $projects;
                
                include "./developmentGroup/getGroups.php";
                $result["allGroups"] = $groups;
                
                include "./boardManagement/getBoards.php";
                $result["allBoards"] = $boards;
                
                $result["success"] = 'true';
                echo json_encode($result);
                die();
            }
            else{
                jsonSuccessDie("users", array(json_decode($user)));
            }
        }
        
    }

    // In default case we return error
    jsonErrorDie('Username or password is incorrect.');